package com.cmc6.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.model.dto.NotNullId;
import com.cmc6.common.util.ApiResult;
import com.cmc6.user.model.dto.UserAnalysisUserNumberDTO;
import com.cmc6.user.model.dto.UserInsertOrUpdateDTO;
import com.cmc6.user.model.dto.UserPageDTO;
import com.cmc6.user.model.dto.UserUpdatePasswordDTO;
import com.cmc6.user.model.vo.UserAnalysisUserNumberVO;
import com.cmc6.user.model.vo.UserPageVO;
import com.cmc6.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户控制器")
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    UserService baseService;

    @ApiOperation(value = "新增/修改 用户")
    @PostMapping("/insertOrUpdate")
    @PreAuthorize("hasAuthority('user:insertOrUpdate')")
    public ApiResult<String> insertOrUpdate(@RequestBody @Valid UserInsertOrUpdateDTO dto) {
        return ApiResult.ok(baseService.insertOrUpdate(dto));
    }

    @ApiOperation(value = "分页排序查询：用户")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('user:page')")
    public ApiResult<Page<UserPageVO>> myPage(@RequestBody @Valid UserPageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @ApiOperation(value = "注销用户")
    @PostMapping("/deleteByIdSet")
    @PreAuthorize("hasAuthority('user:deleteByIdSet')")
    public ApiResult<String> deleteByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.deleteByIdSet(notEmptyIdSet));
    }

    @ApiOperation(value = "通过主键id，查看详情")
    @PostMapping("/infoById")
    @PreAuthorize("hasAuthority('user:infoById')")
    public ApiResult<UserPageVO> infoById(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.infoById(notNullId));
    }

    @ApiOperation(value = "刷新用户 jwt私钥后缀")
    @PostMapping(value = "/refreshJwtSecretSuf")
    @PreAuthorize("hasAuthority('user:insertOrUpdate')")
    public ApiResult<String> refreshJwtSecretSuf(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.refreshJwtSecretSuf(notEmptyIdSet));
    }

    @ApiOperation(value = "重置头像")
    @PostMapping("/resetAvatar")
    @PreAuthorize("hasAuthority('user:insertOrUpdate')")
    public ApiResult<String> resetAvatar(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.resetAvatar(notEmptyIdSet));
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/updatePassword")
    @PreAuthorize("hasAuthority('user:insertOrUpdate')")
    public ApiResult<String> updatePassword(@RequestBody @Valid UserUpdatePasswordDTO dto) {
        return ApiResult.ok(baseService.updatePassword(dto));
    }

    @ApiOperation(value = "统计用户数")
    @PostMapping("/analysisUserNumber")
    @PreAuthorize("hasAuthority('user:page')")
    public ApiResult<List<UserAnalysisUserNumberVO>> analysisUserNumber(
        @RequestBody @Valid UserAnalysisUserNumberDTO dto) {
        return ApiResult.ok(baseService.analysisUserNumber(dto));
    }

}
